package com.sgb.trip.view.adapter;

import android.content.Context;

import com.sgb.trip.R;
import com.sgb.trip.databinding.TripItemFlightPriceBinding;
import com.sgb.trip.entity.FlightInfoEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.NumberUtil;

import java.util.List;

/**
 * Time:2021/6/18
 * Author:黄淋
 * Description:
 */
public class FlightPriceAdapter extends BaseRecycleAdapter<FlightInfoEntity.TicketsBean> {
    protected OnCallBack onCallBack;

    public FlightPriceAdapter(Context context, List<FlightInfoEntity.TicketsBean> datas) {
        super(context, R.layout.trip_item_flight_price, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, FlightInfoEntity.TicketsBean ticketsBean) {
        TripItemFlightPriceBinding binding = (TripItemFlightPriceBinding) holder.getmBinding();
        binding.tvPrice.setText(NumberUtil.format(ticketsBean.getCabinPrice().getAdultFarePrice(), 0));
        if (ticketsBean.getSeatLeftNum() == 0) {
            binding.tvSeatLeftNum.setText("");
        } else if (ticketsBean.getSeatLeftNum() >= 10) {
            binding.tvSeatLeftNum.setText("余票充足");
        } else {
            binding.tvSeatLeftNum.setText(String.format("仅剩%s张", ticketsBean.getSeatLeftNum()));
        }

        binding.tvPriceDiscount.setText(String.format("%s%s", ticketsBean.getCabinName(),
                ticketsBean.getDiscount() >= 1.0 ? "" : (NumberUtil.format(ticketsBean.getDiscount() * 10, 1) + "折")));

        switch (ticketsBean.getTicketType()) {
            case 1:
                binding.tvNextStep.setText("订票");
                break;
            case 2:
                binding.tvNextStep.setText("设为去程");
                break;
            case 3:
                binding.tvNextStep.setText("设为返程");
                break;
        }
        final int position = holder.getIndex_position();
        binding.tvNextStep.setOnClickListener(view -> {
            if (onCallBack != null)
                onCallBack.callBack(ticketsBean.getTicketType(), position);
        });
    }

    public void setOnCallBack(OnCallBack onCallBack) {
        this.onCallBack = onCallBack;
    }

    public interface OnCallBack {
        void callBack(int ticketType, int position);

    }
}
